#!/bin/bash
#
# Copyright (C) 2015 The MoKee OpenSource Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Some command line magic
export QUIET=1
export BRUTECRUSH="-brute"

if [ ! "$BUILD_WITH_COLORS" = "0" ];then
    export CL_RED="\033[31m"
    export CL_GRN="\033[32m"
    export CL_YLW="\033[33m"
    export CL_BLU="\033[34m"
    export CL_MAG="\033[35m"
    export CL_CYN="\033[36m"
    export CL_RST="\033[0m"
fi

if [ -z "$BRUTE_PNGCRUSH" ];then
    export BRUTECRUSH=""
fi

# Get the path of apkcerts.txt
if [ -z "$OUT" -o ! -d "$OUT" ];then
    echo -e $CL_RED"ERROR: $0 only works with a full build environment. $OUT should exist."$CL_RST
    exit 1
fi

if [ -z "$APKCERTS" ];then
    INTER=$(ls $OUT/obj/PACKAGING/target_files_intermediates | grep zip | cut -d'.' -f1)
    export APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$INTER/META/apkcerts.txt

    if [ ! -f "$APKCERTS" ];then
        echo -e $CL_RED"set APKCERTS to the path to your apkcerts.txt file"$CL_RST
        exit 1
    fi
fi

if [ ! -f "$APKCERTS" ];then
    echo -e $CL_RED"Invalid path to apkcerts.txt, set APKCERTS to the correct path."$CL_RST
fi

# Decide which tool to use
if [ "$(which pngquant)" != "" ];then
    optimize_png () {
        pngquant --speed 1 --force --ext .png $1 1> /dev/null 2> /dev/null
    }
elif [ "$(which pngcrush)" != "" ];then
    optimize_png () {
        pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null
        mv ${1}.out ${1}
    }
elif [ "$(which optipng)" != "" ];then
    optimize_png () {
        optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
    }
elif [ "$(which pngout-static)" != "" ];then
    optimize_png () {
        pngout-static $1
    }
elif [ "$(which pngout)" != "" ];then
    optimize_png () {
        pngout $1
    }
else
    echo -e $CL_RED"Please install pngquant, pngcrush, optipng, or pngout"$CL_RST
    exit
fi

export -f optimize_png

# Compulsory tools
if [ "$(which aapt)" = "" ];then
    echo -e $CL_RED"Please ensure aapt is in your \$PATH"$CL_RST
    exit 1
fi

if [ "$(which zipalign)" = "" ];then
    echo -e $CL_RED"Please ensure zipalign is in your \$PATH"$CL_RST
    exit 1
fi

# Notify
echo -e $CL_GRN"--- Starting pngcrusher ---"$CL_RST
echo -e $CL_BLU"apkcerts.txt path: "$CL_RST"$APKCERTS"

# Setup args
OUT_TARGET_HOST=$(uname -s)
if [ x"$OUT_TARGET_HOST" = x"Linux" ];then
    OUT_TARGET_HOST=linux-x86
    XARGS="xargs --max-args=1 --max-procs $(grep 'processor' /proc/cpuinfo|wc -l)"
elif [ x"$OUT_TARGET_HOST" = x"Darwin" ];then
    OUT_TARGET_HOST=darwin-x86
    XARGS="xargs -n 1 -P $(sysctl hw.ncpu | awk '{print $2}')"
else
    echo -e $CL_RED"ERROR: unknown/unsupported host OS!"$CL_RST
    exit 1
fi

# Start crushing
cd $OUT/system
find framework/ app/ priv-app/ vendor/app/ -name \*.apk -print | $XARGS $ANDROID_BUILD_TOP/build/tools/releasetools/crush

echo -e $CL_GRN"--- pngcrusher done ---"$CL_RST
